package think.bank;

public class IndividualAccount implements Account{
	private String accountName;
	private int account;
	
	public IndividualAccount(String accountName,int account){
		this.account=account;
		this.accountName=accountName;
	}
	
	public IndividualAccount(String accountName){
		this(accountName,0);
	}
	
	@Override
	public boolean saveMoney(int money) {
		account  += money;
		return true;
	}

	@Override
	public  boolean getMoney(int money) {
		if(money>account){
			System.out.println(Code.MONEY_NOT_ENOUGH);
			return false;
		}
		
		//线程睡眠1秒 模拟多线程访问对象冲突
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		account -= money;
		return true;
	}
	
	
	public boolean getMoneyThread(int money){
		Thread thread = new Thread(()->{
			System.out.println("线程取钱");
			this.getMoney(money);
			System.out.println("余额:"+account);
		});
		thread.start();
		return true;
	}
	
	
	public String getAccountName() {
		return accountName;
	}

	public void setAccountName(String accountName) {
		this.accountName = accountName;
	}

	public int getAccount() {
		return account;
	}

	public void setAccount(int account) {
		this.account = account;
	}

}
